package lion.blog.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

import lion.framework.core.db.DB;
import lion.framework.core.db.DataTable;
import lion.framework.core.db.paging.SQLPagingFactory;
import lion.framework.core.db.SessionFactory;
import lion.dev.lang.BeanUtil;
import lion.dev.lang.Lang;
import lion.dev.lang.MapJ;

public class BlogArticle implements Serializable{

	private static final long serialVersionUID = 1L;
	public static final String TABLE_NAME = "BlogArticle";

	private static String _SAVESQL="insert into  " + TABLE_NAME + "(title,content,catelog,alias,author,source,sourceLink,publishTime,tags,commentEnable,summary,template,createTime,creator,draft,readCount,logo)values(:title,:content,:catelog,:alias,:author,:source,:sourceLink,:publishTime,:tags,:commentEnable,:summary,:template,:createTime,:creator,:draft,:readCount,:logo)";
	private static String _UPDATESQL="update " + TABLE_NAME + " set title=:title,content=:content,catelog=:catelog,alias=:alias,author=:author,source=:source,sourceLink=:sourceLink,publishTime=:publishTime,tags=:tags,commentEnable=:commentEnable,summary=:summary,template=:template,createTime=:createTime,creator=:creator,draft=:draft,readCount=:readCount,logo=:logo where id=:id";
	private static String _DELETESQL="delete from " + TABLE_NAME + " where id=? ";
	private static String _DELETEALLSQL="delete from " + TABLE_NAME + " where id in(%s) ";
	private static String _GETSQL="select * from " + TABLE_NAME + " where id=? ";
	private static String _LISTSQL="select * from " + TABLE_NAME + " ";
	private static String _COUNTSQL="select count(*) from " + TABLE_NAME + " ";

	
	private int id;
	private String title;
	private String content;
	private int catelog;
	private String alias;
	private String author;
	private String source;
	private String sourceLink;
	private Date publishTime;
	private String tags;
	private boolean commentEnable;
	private String summary;
	private String template;
	private Date createTime;
	private int creator;
	private boolean draft;
	private int readCount;
	private String logo;
	
	public void setId(int id){this.id=id;}
	public int getId(){return this.id;}
	public void setTitle(String title){this.title=title;}
	public String getTitle(){return this.title;}
	public void setContent(String content){this.content=content;}
	public String getContent(){return this.content;}
	public void setCatelog(int catelog){this.catelog=catelog;}
	public int getCatelog(){return this.catelog;}
	public void setAlias(String alias){this.alias=alias;}
	public String getAlias(){return this.alias;}
	public void setAuthor(String author){this.author=author;}
	public String getAuthor(){return this.author;}
	public void setSource(String source){this.source=source;}
	public String getSource(){return this.source;}
	public void setSourceLink(String sourceLink){this.sourceLink=sourceLink;}
	public String getSourceLink(){return this.sourceLink;}
	public void setPublishTime(Date publishTime){this.publishTime=publishTime;}
	public Date getPublishTime(){return this.publishTime;}
	public void setTags(String tags){this.tags=tags;}
	public String getTags(){return this.tags;}
	public void setCommentEnable(boolean commentEnable){this.commentEnable=commentEnable;}
	public boolean isCommentEnable(){return this.commentEnable;}
	public void setSummary(String summary){this.summary=summary;}
	public String getSummary(){return this.summary;}
	public void setTemplate(String template){this.template=template;}
	public String getTemplate(){return this.template;}
	public void setCreateTime(Date createTime){this.createTime=createTime;}
	public Date getCreateTime(){return this.createTime;}
	public void setCreator(int creator){this.creator=creator;}
	public int getCreator(){return this.creator;}
	public void setDraft(boolean draft){this.draft=draft;}
	public boolean isDraft(){return this.draft;}
	public void setReadCount(int readCount){this.readCount=readCount;}
	public int getReadCount(){return this.readCount;}
	public void setLogo(String logo){this.logo=logo;}
	public String getLogo(){return this.logo;}

	
	public void save() {

		DB db = SessionFactory.getCurrentDB();
		this.save(db);
	}

	public void save(DB db) {

		DataTable dt = db.save(_SAVESQL, this.valueMap());
		if (dt.getRowCount() > 0) {
			MapJ row = dt.getRow(0);
			this.id = row.getInt(row.getKey(0));
		}
	}

	public void update() {

		DB db = SessionFactory.getCurrentDB();
		this.update(db);

	}

	public void update(DB db) {

		db.update(_UPDATESQL, this.valueMap());
	}

	public void delete() {

		DB db = SessionFactory.getCurrentDB();
		this.delete(db);
	}

	public void delete(DB db) {

		db.update(_DELETESQL, this.getId());
	}

	public static void delete(DB db, int... ids) {

		if (ids == null || ids.length == 0) { return; }
		String sql = String.format(_DELETEALLSQL, Lang.join(ids));
		db.update(sql);
	}

	public static void delete(int... ids) {

		delete(SessionFactory.getCurrentDB(), ids);
	}
	
	public static BlogArticle load(int id) {

		DB db = SessionFactory.getCurrentDB();
		return load(id, db);
	}

	public static BlogArticle load(int id, DB db) {

		MapJ row = db.queryOneRow(_GETSQL, id);

		if(row == null){return null;}
		return BlogArticle.create(row);

	}
	
	public static BlogArticle load(String where) {

		DB db = SessionFactory.getCurrentDB();
		return load(where, db);
	}

	public static BlogArticle load(String where, DB db) {

		return load(where, null, db);
	}

	public static BlogArticle load(String where,MapJ param, DB db) {

		String sql = _LISTSQL + (StringUtils.isBlank(where) ? "" : where);

		MapJ row = db.queryOneRow(sql,param);
		if(row == null){return null;}

		return BlogArticle.create(row);
	}

	public static int count() {

		return count("");
	}

	public static int count(DB db) {

		return count("", db);
	}

	public static int count(String where) {

		return count(where, SessionFactory.getCurrentDB());
	}

	public static int count(String where, MapJ param) {

		return count(where, param, SessionFactory.getCurrentDB());
	}

	public static int count(String where, DB db) {

		return count(where, null, db);
	}

	public static int count(String where, MapJ param, DB db) {

		String sql = _COUNTSQL + (StringUtils.isBlank(where) ? "" : " " + where);
		return db.queryInt(sql, param);

	}
	public static BlogArticle create(MapJ map) {

		BlogArticle o = new BlogArticle();
		try {
			BeanUtil.populate(o, map);
		} catch (Exception e) {
		}

		return o;
	}

	public static List<BlogArticle> list(int size) {

		DB db = SessionFactory.getCurrentDB();
		return list(size, db);
	}

	public static List<BlogArticle> list(int size, DB db) {

		return list(size, null, db);
	}

	public static List<BlogArticle> list(int size, String where) {

		return list(size, where, SessionFactory.getCurrentDB());
	}

	public static List<BlogArticle> list(int size, String where, DB db) {

		return list(1, size, where, db);
	}

	public static List<BlogArticle> list(int size, String where, MapJ param, DB db) {

		return list(1, size, where, param, db);
	}

	public static List<BlogArticle> list(int start, int size) {

		return list(start, size, SessionFactory.getCurrentDB());
	}

	public static List<BlogArticle> list(int start, int size, DB db) {

		return list(start, size, null, db);
	}

	public static List<BlogArticle> list(int start, int size, String where) {

		return list(start, size, where, SessionFactory.getCurrentDB());
	}

	public static List<BlogArticle> list(int start, int size, String where, DB db) {

		return list(start, size, where, null, db);
	}

	public static List<BlogArticle> list(int start, int size, String where, MapJ param, DB db) {

		String sql = SQLPagingFactory.paging(db.getType(), _LISTSQL + " " + (StringUtils.isBlank(where) ? "" : where), start, size);

		DataTable dt = null;
		if (param == null) {
			dt = db.query(sql);
		} else {
			dt = db.query(sql, param);
		}

		List<BlogArticle> result = new ArrayList<>();

		for (MapJ row : dt.getRows()) {
			result.add(BlogArticle.create(row));
		}

		return result;
	}

	public MapJ valueMap() {

		MapJ mapJ = new MapJ();
		
		mapJ.put("id", this.id);
		mapJ.put("title", this.title);
		mapJ.put("content", this.content);
		mapJ.put("catelog", this.catelog);
		mapJ.put("alias", this.alias);
		mapJ.put("author", this.author);
		mapJ.put("source", this.source);
		mapJ.put("sourceLink", this.sourceLink);
		mapJ.put("publishTime", this.publishTime);
		mapJ.put("tags", this.tags);
		mapJ.put("commentEnable", this.commentEnable);
		mapJ.put("summary", this.summary);
		mapJ.put("template", this.template);
		mapJ.put("createTime", this.createTime);
		mapJ.put("creator", this.creator);
		mapJ.put("draft", this.draft);
		mapJ.put("readCount", this.readCount);
		mapJ.put("logo", this.logo);
		
		return mapJ;
	}
}